<?php
namespace app\admin\validate;

use think\Validate;
use app\admin\model\FlowConfig as FlowConfigModel;

class FlowConfig extends Validate
{
    protected $rule = [
        'type_id' => 'require',
        'node_id' => 'require|checkNode',
        'step' => 'require|checkStep|number',
        'type' => 'checkRole',
        'work_day' => 'require|number'
        
    ];
    
    protected $message = [
        'type_id.require' => '流程类型不能为空！',
        'ver_id.require' => '流程版本不能为空！',
        'node_id.require' => '流程节点不能为空！',
        'node_id.checkNode' => '节点配置已存在！',
        'step.require' => '步骤不能为空！',
        'step.checkStep' => '步骤配置已存在！',
        'step.number'=>'步骤必须是数字！',
        'type.checkRole' => '流程办理人、组、SQL必须填写',
        'work_day.require' => '工作时间不能为空',
        'work_day.number' => '工作时间必须为数字'
    ];
    
    public function checkNode($value, $rule, $data) 
    {
        $where = [
            'type_id' => $data['type_id'],
            'node_id' => $value,
            'isvalid' => 1
        ];
        if (isset($data['id'])) {
            $where['id'] = ['neq', $data['id']];
        }
        $lists = (new FlowConfigModel())->where($where)->find();
        
        return $lists ? false : true;
    }
    
    public function checkStep($value, $rule, $data) 
    {
        $where = [
            'type_id' => $data['type_id'],
            'step' => $value,
            'isvalid' => 1
        ];
        if (isset($data['id'])) {
            $where['id'] = ['neq', $data['id']];
        }
        $lists = (new FlowConfigModel())->where($where)->find();

        return $lists ? false : true;
    }
    
    public function checkRole($value, $rule, $data)
    {
        if ($data['type'] == 2) {
            return $data['sql_text'] ?  true : false;
        }  else {
            return $data['role_id'] ? true : false;
        }
    }
}
